package recomendation.persist;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import recomendation.Tag;


public class DAO {
	
	public static Connection conn = null; 
	
	public static void connect(){

		 conn = null;

        try
        {
            String userName = "root";
            String password = "";
            String url = "jdbc:mysql://localhost/ria";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
 	}
	
	public static void disconnect(){
		if (conn != null)
	    {
	        try
	        {
	        	conn.close ();
	            System.out.println ("Database connection terminated");
	        }
	        catch (Exception e) {  }
	    }
	}
	
	public static ArrayList<Tag> topTenWords(){
		ArrayList<Tag> tags= new ArrayList<Tag>();
		
		PreparedStatement s;
		ResultSet rs=null;
		
		  try {  
			  if(conn!=null){
				  s = conn.prepareStatement (
					           "SELECT score, text from recomendacion ORDER BY score DESC LIMIT 10");
				 
				  rs = s.executeQuery ();
				  
				  while (rs.next()){
					  tags.add(new Tag(rs.getString("text"),rs.getInt("score")));
				  }
			  }
		 } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return tags;
	} 
	
	public static boolean insert(Tag t){
		 PreparedStatement s;
		 int count=0;
		  try {  
			  if(conn!=null){
				  s = conn.prepareStatement (
					           "INSERT INTO recomendacion (text, score) VALUES(?,?)");
				  s.setString (1, t.getText());
				  s.setInt(2, t.getScore());
				  count = s.executeUpdate ();
			  }
		 } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		   
		return count!=0?true:false;
	}
	
	public static boolean updateTagCloud(Tag t){
		boolean b= udpdate(t);
		
		PreparedStatement s;
		
		if (!b){
			t.setScore(1);
			return insert(t);
		}
		else{
			return b;
		}

		
	}
	
	public static boolean udpdate(Tag t){
		PreparedStatement s;
		ResultSet rs=null;
		int count=0;
		int score=0;
		  try {  
			  if(conn!=null){
				  s = conn.prepareStatement (
					           "SELECT * from recomendacion WHERE text=?");
				  s.setString (1, t.getText());
				 
				  rs = s.executeQuery ();
				  
				  if (rs.next()){
					  
					  score= rs.getInt("score");
					  
					  s = conn.prepareStatement (
			           		"UPDATE recomendacion SET score=? WHERE text=?");
					  s.setInt(1, score+1);
					  s.setString (2, t.getText());
					  
					  count = s.executeUpdate ();
				  }
			  }
		 } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		   
		return count!=0?true:false;
	}

	
	
}
